package com.noah.note.presenter;

import com.noah.note.R;
import com.noah.note.bean.Reply;
import com.noah.note.bean.ReplyNote;
import com.noah.note.global.BasePresenter;
import com.noah.note.global.Const;
import com.noah.note.global.OkHttpCallback;
import com.noah.note.manager.JsonManager;
import com.noah.note.model.NoteModel;
import com.noah.note.utils.LogUtils;
import com.noah.note.utils.NetUtils;
import com.noah.note.utils.ToastUtils;
import com.noah.note.view.NoteView;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class NotePresenter extends BasePresenter<NoteView> {
    NoteModel model;

    public NotePresenter() {
        model = new NoteModel();
    }

    public void getNoteList(long bid, long time, final boolean isRefresh) {
        if (!NetUtils.isConnected()) {//无网络
            ToastUtils.show(R.string.failure_network);
            return;
        }
        model.getNoteList(bid, time, new OkHttpCallback() {
            @Override
            public void onFail(Reply reply) {
                switch (reply.getCode()) {
                    case Const.FAIL:
                        //ToastUtils.show(R.string.failure_request);
                        break;
                    case Const.FAIL_REQUEST:
                        ToastUtils.show(R.string.failure_request);
                        break;
                }
            }

            @Override
            public void onSuccess(Reply reply) {
                String data = reply.getData();
                ArrayList<ReplyNote> replyNotes = (ArrayList<ReplyNote>) JsonManager.getInstance().toBeanList(data, ReplyNote.class);
                LogUtils.i(replyNotes.toString());
                if (getView() != null) {
                    if(isRefresh){
                        getView().noteListRefreshSuccess(replyNotes);
                    }else {
                        getView().noteListSuccess(replyNotes);
                    }
                }
            }
        });
    }
}
